package com.ydd.moudle.home;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.taofengsdk.okhttp.listener.DisposeDataListener;
import com.ydd.R;
import com.ydd.adapter.StoryCommentAdapter;
import com.ydd.base.BaseActivity;
import com.ydd.model.story.BaseStoryModel;
import com.ydd.model.story.StoryCommentValue;
import com.ydd.moudle.me.LoginActivity;
import com.ydd.network.http.RequestCenter;
import com.ydd.util.Util;
import com.ydd.util.db.SPManager;
import com.ydd.widegt.story.StoryDeailHeaderView;

/**
 * Created by tao on 17-3-18.
 * 課程詳情頁面
 */

public class StoryDetailActivity extends BaseActivity implements View.OnClickListener {
    public static String STORY_ID = "storyID";

    /**
     * UI
     */
    private ImageView mBackView;
    private RecyclerView mCommentRecycleView;
    private ImageView mLoadingView;
    private RelativeLayout mBottomLayout;
    private ImageView mJianPanView;
    private EditText mInputEditView;
    private TextView mSendView;
    private StoryDeailHeaderView headerView;
    //        private CourseDetailFooterView footerView;
    private StoryCommentAdapter mAdapter;
    /**
     * Data
     */
    private String mStoryID;
    private BaseStoryModel mData;
    private String tempHint = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_detail_layout);

        initData();
        initView();
        requestDeatil();
    }

    private void initView() {
        initHeaderView(getString(R.string.story_detail), true);

        mBackView = (ImageView) findViewById(R.id.back_view);
        mBackView.setOnClickListener(this);
        mCommentRecycleView = (RecyclerView) findViewById(R.id.comment_recycle_view);
        mCommentRecycleView.setVisibility(View.GONE);
        mLoadingView = (ImageView) findViewById(R.id.loading_view);
        mLoadingView.setVisibility(View.VISIBLE);
        AnimationDrawable anim = (AnimationDrawable) mLoadingView.getDrawable();
        anim.start();

        mBottomLayout = (RelativeLayout) findViewById(R.id.bottom_layout);
        mJianPanView = (ImageView) findViewById(R.id.jianpan_view);
        mJianPanView.setOnClickListener(this);
        mInputEditView = (EditText) findViewById(R.id.comment_edit_view);
        mSendView = (TextView) findViewById(R.id.send_view);
        mSendView.setOnClickListener(this);
        mBottomLayout.setVisibility(View.GONE);

        intoEmptyState();
    }

    private void initData() {
        Intent intent = getIntent();
        mStoryID = intent.getStringExtra(STORY_ID);
    }

    /**
     * EditText进入编辑状态
     */
    private void intoEditState(String hint) {
        mInputEditView.requestFocus();
        mInputEditView.setHint(hint);
        Util.showSoftInputMethod(this, mInputEditView);
    }

    public void intoEmptyState() {
        tempHint = "";
        mInputEditView.setText("");
        mInputEditView.setHint(getString(R.string.input_comment));
        Util.hideSoftInputMethod(this, mInputEditView);
    }

    private void requestDeatil() {

        RequestCenter.requestStoryDetail(mStoryID, new DisposeDataListener() {
            @Override
            public void onSuccess(Object responseObj) {
                mData = (BaseStoryModel) responseObj;
                updateUI();
            }

            @Override
            public void onFailure(Object reasonObj) {

            }
        });
    }

    //根据数据填充UI
    private void updateUI() {
        mLoadingView.setVisibility(View.GONE);
        mCommentRecycleView.setVisibility(View.VISIBLE);
        mCommentRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new StoryCommentAdapter(this, mData.data.body);
        mCommentRecycleView.setAdapter(mAdapter);

        headerView = new StoryDeailHeaderView(this, mData.data.head);
        mAdapter.addHeaderView(headerView);
//        if (footerView != null) {
//            mListView.removeFooterView(footerView);
//        }
//        footerView = new CourseDetailFooterView(this, mData.data.footer);
//        mListView.addFooterView(footerView);

        mBottomLayout.setVisibility(View.VISIBLE);

        onItemClick();
    }

    private StoryCommentValue assembleCommentValue(String comment) {
        String name = SPManager.getInstance().getString(SPManager.USER_NAME, "");
        StoryCommentValue value = new StoryCommentValue();
        value.name = name;
//        value.logo = user.data.photoUrl;
        value.text = tempHint + comment;
        return value;
    }

    private void onItemClick() {
        mAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int position) {
                boolean isLogin = SPManager.getInstance().getBoolean(SPManager.IS_LOGIN_USER, false);
                if (isLogin) {
                    StoryCommentValue value = (StoryCommentValue) mAdapter.getItem(
                            position);

                    //不是自己的评论，可以回复
                    tempHint = getString(R.string.comment_hint_head).concat(value.name).
                            concat(getString(R.string.comment_hint_footer));
                    intoEditState(tempHint);
                } else {
                    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.send_view:
                String comment = mInputEditView.getText().toString().trim();
                boolean isLogin = SPManager.getInstance().getBoolean(SPManager.IS_LOGIN_USER, false);
                if (isLogin) {
                    if (!TextUtils.isEmpty(comment)) {
                        mAdapter.addComment(assembleCommentValue(comment));
                        intoEmptyState();
                    } else {
                        Toast.makeText(this, "内容不能为空!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    startActivity(new Intent(this, LoginActivity.class));
                }

                break;
            case R.id.jianpan_view:
                mInputEditView.requestFocus();
                Util.showSoftInputMethod(this, mInputEditView);
                break;
        }

    }
}
